package com.wcity.manage.service.community.social.impl;

import com.wcity.manage.common.enums.SaveImageType;
import com.wcity.manage.common.vo.community.social.SocialInfoVo;
import com.wcity.manage.dao.mapper.community.social.SocialInfoMapper;
import com.wcity.manage.dao.model.community.image.ImageUrl;
import com.wcity.manage.dao.model.community.social.SocialInfo;
import com.wcity.manage.service.community.image.ImageService;
import com.wcity.manage.service.community.social.SocialInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 社工
 * Created by hgg on 2016/12/20.
 */
@Service
public class SocialInfoServiceImpl implements SocialInfoService {

    @Autowired
    private SocialInfoMapper socialInfoMapper;
    @Autowired
    private ImageService imageService;

    /**
     * 社工列表
     * @param queryMap
     * @return
     */
    @Override
    public List<SocialInfoVo> socialInfoPageList( Map<String, Object> queryMap ) {

        return socialInfoMapper.socialInfoPageList( queryMap );
    }

    /**
     * 删除社工
     * @param deleteMap
     * @return
     */
    @Transactional( propagation = Propagation.REQUIRED, isolation = Isolation.SERIALIZABLE,rollbackForClassName = "Exception" )
    @Override
    public Integer deleteSocialInfo( Map<String, Object> deleteMap ) {

        //【1】删除社工
        Map<String, Object> deleteImageMap = new HashMap<>();
        deleteImageMap.put( "referId", deleteMap.get( "id" ) );
        deleteImageMap.put( "saveType", SaveImageType.COMMUNITY_SOCIALINFO );
        imageService.deleteImage( deleteImageMap );
        //【2】删除社工信息
        Integer delNum = socialInfoMapper.deleteSocialInfo( deleteMap );
        if( delNum == null ){
            throw new RuntimeException("删除社工信息失败");
        }
        return 1;
    }

    /**
     * 新增社工
     * @param socialInfo
     * @return
     */
    @Transactional( propagation = Propagation.REQUIRED, isolation = Isolation.SERIALIZABLE,rollbackForClassName = "Exception" )
    @Override
    public Integer saveAddSocialInfo( SocialInfo socialInfo,String imgUrl ) {

        Integer saveNum = socialInfoMapper.saveAddSocialInfo( socialInfo );
        if( saveNum == 0 ){
            throw new RuntimeException("新增社工信息失败");
        }
        //【2】保存关联封面图片
//        ImageUrl image = new ImageUrl( null,imgUrl,null,socialInfo.getId(), SaveImageType.COMMUNITY_SOCIALINFO );
//        Integer saveImageNum = imageService.saveAddImage( image );
//        if( saveImageNum == 0 ){
//            throw new RuntimeException( "保存社工信息封面图片失败" );
//        }
        return 1;
    }

    /**
     * 更新社工
     * @param socialInfo
     * @return
     */
    @Override
    public Integer saveUpdateSocialInfo( SocialInfo socialInfo ) {

        return socialInfoMapper.saveUpdateSocialInfo( socialInfo );
    }

    /**
     * 社工详情
     * @param detailMap
     * @return
     */
    @Override
    public SocialInfoVo socialInfoDetail( Map<String, Object> detailMap ) {

        return socialInfoMapper.socialInfoDetail( detailMap );
    }

    /**
     * 社工总数量
     * @param queryMap
     * @return
     */
    @Override
    public Integer totalSocialInfo( Map<String, Object> queryMap ) {

        return socialInfoMapper.totalSocialInfo( queryMap );
    }

    /**
     * 社工列表非分页
     * @param queryMap
     * @return
     */
    @Override
    public List<SocialInfoVo> socialInfoList( Map<String, Object> queryMap ) {

        return socialInfoMapper.socialInfoList( queryMap );
    }

    /**
     * 查询小区ID拼接（1,2,3）
     * @param queryMap
     * @return
     */
    public String blockIds( Map<String,Object> queryMap ){

        return socialInfoMapper.blockIds( queryMap );
    }
}
